﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.MeetingRecoreds.BLL;
using Yipusoft.MeetingRecoreds.Model;

public partial class Task_MeetingRecord_MeetingList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }
    void init()
    {

        BindDepartmentId();
        BindUnit();
        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (DepartmentId.Substring(0, 4) != "1300" && DepartmentId.Substring(4, 1) == "0")
        {
            ddlDepartment.SelectedValue = DepartmentId.Substring(0, 4) + "000";
            BindUnit();
            ddlDepartment.Enabled = false;

        }
        else if (DepartmentId.Substring(0, 4) != "1300" && (DepartmentId.Substring(4, 1) == "3" || DepartmentId.Substring(4, 1) == "2"))
        {
            ddlDepartment.SelectedValue = DepartmentId.Substring(0, 4) + "000";
            BindUnit();
            ddlUnit.SelectedValue = DepartmentId;
            ddlDepartment.Enabled = false;
            ddlUnit.Enabled = false;
        }
        ListPageContral1.SetFirstPage();
        BindData(0);

    }

    void BindData(int pageIndex)
    {
        string DepartmentId = ddlUnit.SelectedValue;
        string where = " departmentId='" + DepartmentId + "' and year(MeetingTime)=" + ddlYear.SelectedValue + " and month(MeetingTime)=" + ddlMonth.SelectedValue + " ";
        if (ddlType.SelectedValue != "")
        {
            where += "and MeetingType=" + ddlType.SelectedValue + "";
        }

        ListPageContral1.ItemCount = MeetingRecored.GetCount(where);
        //where += "order by setTime desc";
        gvMeeting.DataSource = MeetingRecored.GetList(where, ListPageContral1.PageSize, pageIndex);
        gvMeeting.DataBind();
        if (Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Trim() != DepartmentId.Trim())
        {
            gvMeeting.Columns[5].Visible = false;
        }

    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        BindData(PageIndex);
    }

    void BindDepartmentId()
    {
        string where = "DepartmentId like '____000' and departmentId<>'1300000' ";
        ddlDepartment.DataSource = Department.GetList(where);
        ddlDepartment.DataBind();

    }

    void BindUnit()
    {
        ddlUnit.Items.Clear();
        string where = "DepartmentId like '" + ddlDepartment.SelectedValue.Substring(0, 4) + "3__' or  DepartmentId like '" + ddlDepartment.SelectedValue.Substring(0, 4) + "2__' or (departmentid in('1302016','1303101','1304103','1305014','1306012','1307012','1308016','1309012','1310101','1311012') and left(DepartmentId,4)='" + ddlDepartment.SelectedValue.Substring(0, 4) + "')";
        ddlUnit.DataSource = Department.GetList(where);
        ddlUnit.DataBind();
    }
    protected void gvMeeting_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((ListPageContral1.CurrentPageIndex * ListPageContral1.PageSize) + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            HyperLink lblSubject = e.Row.FindControl("lblSubject") as HyperLink;
            Label lblMeetingType = e.Row.FindControl("lblMeetingType") as Label;
            MeetingRecordInfo MRInfo = e.Row.DataItem as MeetingRecordInfo;
            int T = MRInfo.MeetingType;
            string MeetingType = "";
            if (T == 0)
            {
                MeetingType = "安全防事故工作记录";
                lblSubject.NavigateUrl = "MeetingType0.aspx?ID=" + MRInfo.Id + "";
            }
            if (T == 1)
            {
                MeetingType = "队伍管理形势分析会";
                lblSubject.NavigateUrl = "MeetingType1.aspx?ID=" + MRInfo.Id + "";
            }
            if (T == 2)
            {
                MeetingType = "队务会";
                lblSubject.NavigateUrl = "MeetingType2.aspx?ID=" + MRInfo.Id + "";
            }
            if (T == 3)
            {
                MeetingType = "班务会";
                lblSubject.NavigateUrl = "MeetingType3.aspx?ID=" + MRInfo.Id + "";
            }
            lblMeetingType.Text = MeetingType;
            lblSubject.Text = MRInfo.Subject;


        }
    }
    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindUnit();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        BindData(0);
    }
    protected void gvMeeting_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Ed")
        {
            Guid Id = new Guid(e.CommandArgument.ToString());
            MeetingRecordInfo info = MeetingRecored.GetDetail(Id);
            Response.Redirect("NewMeeting.aspx?T=" + info.MeetingType + "&ID=" + Id + "");
        }
        if (e.CommandName == "Del")
        {
            Guid Id = new Guid(e.CommandArgument.ToString());
            MeetingRecored.Delete(Id);
            init();
        }
    }
}
